/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.util.review;

import java.io.Serializable;
import java.util.Comparator;

/**
 * Compares two id items
 *
 */
public class IdComparator implements Comparator<AbstractIdItem>,
		Serializable {
	private static final long serialVersionUID = 8371982097896487673L;

	@Override
	public int compare(AbstractIdItem o1, AbstractIdItem o2) {
		if(o1 == null && o2 == null)
			return 0;
		
		if(o1 == null)
			return -1;
		
		if(o2 == null)
			return 1;
		
		Integer index1 = null;
		Integer index2 = null;
		try {
			index1 = Integer.parseInt(o1.getId());
		} catch(NumberFormatException e) {
			// ignore
		}
		
		try {
			index2 = Integer.parseInt(o2.getId());
		} catch(NumberFormatException e) {
			// ignore
		}
		
		if(index1 == null && index2 == null)
			return 0;
		
		if(index1 == null)
			return -1;
		
		if(index2 == null)
			return 1;
		
		return index1 - index2;
	}
}